package com.glory.brepda.activity.tool;


import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

public class ToolScrapActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    // 治具编号输入框
    @BindView(R.id.etToolID)
    FreeUI_ClearEditText etToolID;
    // 治具报废原因下拉选择框
    // fl(FrameLayout)、tv(TextView)、iv(ImageView)
    @BindView(R.id.flToolScrapReason)
    FrameLayout flToolScrapReason;
    @BindView(R.id.tvToolScrapReason)
    TextView tvToolScrapReason;
    @BindView(R.id.ivToolScrapReason)
    ImageView ivToolScrapReason;

    // 报废原因下拉框内容
    private FreeUI_GeneralPop toolScrapReasonPop;
    private ArrayList<String> toolScrapReasonList = new ArrayList<>();


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_tool_scrap;
    }

    @Override
    protected void initView() {
        tvTitle.setText("治具报废");
        setMainLayout(llMain);
    }

    @Override
    protected void initData() {
        // 治具报废原因，目前暂定写死原因（目前没有必要写在用户栏位参考值里，后续有必要再去处理
        toolScrapReasonList.addAll(Arrays.asList("使用时间超过2年", "检验有甭损"));
    }

    @OnClick({R.id.tvBaseSubmit, R.id.flToolScrapReason})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit: {
                // 检查治具编号有没有输入和治具报废原因有没有选择
                if (StringUtils.isEmpty(etToolID.getText())
                    || StringUtils.isEmpty(tvToolScrapReason.getText())) {
                    showToast("治具编号或报废原因不能为空", false);
                    return;
                }

                // 提交请求
                FreeUI_EntityCallBack<BaseResponseBean<Object>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<Object>>
                        (new TypeToken<BaseResponseBean<Object>>() {
                        }.getType(),getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<Object>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(ToolScrapActivity.this, false, getSupportFragmentManager())) {
                            showToast("提交成功",true);
                            etToolID.setFocusable(true);
                            etToolID.setText("");
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<Object>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "TOOLCLEAR");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", "SCRAP");
                map.addProperty("TOOLID", String.valueOf(etToolID.getText()));
                map.addProperty("TOOLSCRAPREASON", tvToolScrapReason.getText().toString());
                NetUtils.requestPostNet("IF.TOOLSCRAP", "Common", ToolScrapActivity.this, map, callBack);

                break;
        }

            case R.id.flToolScrapReason: {
                // 下拉框选你
                if (toolScrapReasonPop != null) {
                    if (toolScrapReasonPop.isShowing()) {
                        toolScrapReasonPop.dismiss();
                    }
                } else {
                    toolScrapReasonPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flToolScrapReason.getWidth(), toolScrapReasonList, ivToolScrapReason, (codeS, i) -> {
                        tvToolScrapReason.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivToolScrapReason);
                toolScrapReasonPop.showAsDropDown(flToolScrapReason, 0, 0);

                break;
            }
        }
    }

}